package com.chinanet.netelementmodel;

import com.chinanet.entry.netelement.Attr;
import com.chinanet.entry.netelement.AttrType;
import com.chinanet.entry.netelement.WorkInterFace;
import com.chinanet.utils.webservice.AttrsIn;
import com.chinanet.utils.webservice.ITMSWebService;

import java.util.HashMap;
import java.util.Map;

/**
 * ITMS_WebService_Imp_unbindWorkTicket
 * itms-设备解绑定操作
 * @author xuzhi
 *
 */
public class ITMS_WebService_Imp_unbindWorkTicket implements WorkInterFace {

	@Override
	public String getName() {
		return "ITMS-设备解绑定操作";
	}

	@Override
	public String getDesciption() {
		return "";
	}

	@Override
	public int getVersion() {
		return 0;
	}

	@Override
	public Attr[] getInAttrs() {
		Attr[] in=new Attr[3];
		in[0]=new Attr("logic_id","终端SN号(两个参数选填一个)", AttrType.TYPE_String,true);
		in[1]=new Attr("serial_number","设备序列号(两个参数选填一个)",AttrType.TYPE_String,true);
		in[2]=new Attr("keep_user_info","是否保留用户信息；0：不保留用户信息1：保留用户信息(必填)",AttrType.TYPE_String,true);
		return in;
	}

	@Override
	public Attr[] getOutAttrs() {
		Attr[] out = new Attr[6];
		out[0]=new Attr("ReturnCode", "0：成功；其它：失败", AttrType.TYPE_Int, true);
		out[1]=new Attr("return", "返回return", AttrType.TYPE_String, true);
		out[2]=new Attr("result", "执行结果，0：执行成功；-1：执行失败", AttrType.TYPE_String, true);
		out[3]=new Attr("err_msg", "出错原因描述", AttrType.TYPE_String, true);
		out[4]=new Attr("log", "服务器返回的soapxml", AttrType.TYPE_String, true);
		out[5]=new Attr("Errormsg", "服务器返回的错误信息,当ReturnCode为0的时候就不必去读取", AttrType.TYPE_String, false);
		return out;
	}

	@Override
	public Map<String, Object> init(Map<String, Object> data) {
		AttrsIn attrsIn=new AttrsIn();
		ITMSWebService webService=new ITMSWebService();
		Map<String,Object> map=new HashMap<String, Object>();
		//Get into arguments
		attrsIn.setFunctionName("unbindWorkTicket");
		attrsIn.setRequestMessage("<logic_id>"+webService.dealString((String) data.get("logic_id"))+"</logic_id>" +
	            "<serial_number>"+webService.dealString((String) data.get("serial_number"))+"</serial_number>" +
	            "<keep_user_info>"+webService.dealString((String) data.get("keep_user_info"))+"</keep_user_info>");
		//Get soapxml
		try {
			String returnXml = webService.WebServiceInit(attrsIn, 1);
			String flag = webService.getPattern("return", returnXml);

			if (flag.length() > 0) {
				map.put("ReturnCode", 0);
				map.put("return", webService.getPattern("return", returnXml));
				map.put("result", webService.getPattern("result", returnXml));
				map.put("err_msg", webService.getPattern("err_msg", returnXml));
				map.put("log", returnXml);
				map.put("Errormsg", "");
			} else {
				map.put("ReturnCode", 1);
				map.put("return", "");
				map.put("result", "");
				map.put("err_msg", "");
				map.put("log", returnXml);
				map.put("Errormsg", "网元侧执行出错，处理请求失败");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return map;
	}

}
